package com.huanying.framework.adminuser;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.aliyuncs.exceptions.ClientException;
import com.huanying.framework.utils.MD5;
import com.huanying.framework.utils.SmsUtility;

@Controller
public class AdminuserController {
	@Autowired
	private AdminuserService userService;
	private static Logger logger = Logger.getLogger(AdminuserController.class);

	@RequestMapping(value = "/login.do")
	public String login() {
		return "/adminuser/login";
	}

	// 处理登录传过来的数据检查用户
	@RequestMapping(value = "/dologin.do")
	public String dologin(HttpServletRequest request, String name, String password, Model model) {

		HttpSession session = request.getSession();
		Adminuser user;
		try {
			user = userService.checkuser(name);
			if (user != null) {
				String salt = user.getSalt();
				if (salt != null) {
					password = MD5.MD5Encode(password+salt);
					// 比对加密后的密码与数据库中的密码
					if (password.equals(user.getPassword())) {
						session.setAttribute("user", user);
						if (user.getRole() == 1) {
							return "redirect:/show_job.do";
						} else if (user.getRole() == 2) {
							return "redirect:/add_jobitem_call.do?user_id=" + user.getId();
						} else if (user.getRole() == 3) {
							return "redirect:/get_authenticPer.do";
						}else if(user.getRole()==4)
						{
							return "redirect:/show_add_store.do";
						}
						logger.info("后台用户登陆成功");
					} else {
						model.addAttribute("mes", "2");
						return "/adminuser/login";
					}
				} else if (salt == null) {
					if (password.equals(user.getPassword())) {

						session.setAttribute("user", user);
						if (user.getRole() == 1) {
							return "redirect:/show_add_job.do";
						} else if (user.getRole() == 2) {
							return "redirect:/add_jobitem_call.do?user_id=" + user.getId();
						} else if (user.getRole() == 3) {
							return "redirect:/get_authenticPer.do";
						}else if(user.getRole()==4)
						{
							return "redirect:/show_add_store.do";
						}
					}
				}
			} else {
				model.addAttribute("mes", "1");
				return "/adminuser/login";
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.error("用户验证出错", e);
		}

		return "";
	}
//用户修改密码
	@RequestMapping(value = "/update_adminuser.do")
	@ResponseBody
	public Map<String, String> update_adminuser(@RequestBody Adminuser user2, String password, String oldpwd,
			HttpSession session) {
		Adminuser user1;
		Map<String, String> map = new HashMap<String, String>();
		String pwd = null;
		if (user2.getId() != 0) {

			user1 = (Adminuser) session.getAttribute("user");
			String salt1 = user1.getSalt();
			user2.setRole(user1.getRole());
				user2.setProvince(user1.getProvince());
				user2.setCity(user1.getCity());
				user2.setCounty(user1.getCounty());
			    user2.setSalt(salt1);
			if (oldpwd != "") {
				if (salt1 == null) {
					pwd = oldpwd;
				} else {
					pwd = MD5.MD5Encode(oldpwd+salt1);
				}
				if (user1.getPassword().equals(pwd)) {
					if (password != "") {
						// 生成5位随机数
						StringBuffer flag = new StringBuffer();
						for (int i = 0; i <= 50; i++) {
							String sources = "0123456789"; // 加上一些字母，就可以生成pc站的验证码了
							Random rand = new Random();
							for (int j = 0; j < 5; j++) {
								flag.append(sources.charAt(rand.nextInt(9)) + "");
							}
						}
						String salt = flag.toString();
						user2.setSalt(salt);
						password = MD5.MD5Encode(password+salt);
						logger.info("修改密码成功");
						user2.setPassword(password);
					}
					try {
						userService.update(user2);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						logger.error("更新失败", e);
					}
					session.setAttribute("user", user2);
					map.put("flag", "1");
				} else {
					map.put("flog", "3");
				}
			} else {
				if (password == "") {
					user2.setPassword(user1.getPassword());
				}
				try {
					userService.update(user2);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					logger.error("更新失败", e);
				}
				session.setAttribute("user", user2);
				map.put("flag", "1");
			}
		}
		return map;
	}

	@RequestMapping(value = "/delete_adminuser.do")
	public String delete_adminuser(String user_id) {
		try {
			userService.delete(Integer.parseInt(user_id));
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			logger.error("删除管理用户失败", e);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.error("删除管理用户失败", e);
			e.printStackTrace();
		}
		return "redirect:/login.do";
	}
	
}
